package com.example.musicplayer.dialogTools;

import android.content.Context;
import android.text.InputFilter;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.example.musicplayer.R;

public class TimingSetDialog extends BaseDialog {
    private static final int NUMBER_LENGTH_LIMIT = 4;
    private static final int TIMING_SET_MAX = 24 * 60;
    private EditText mEditText;
    private Button mConfirm;

    public TimingSetDialog(Context context) {
        super(context, R.style.NormalDialogStyle_Bottom);
    }

    @Override
    protected View setDialogView(Context context) {
        View view = View.inflate(context, R.layout.dialog_timing_set, null);
        mEditText = view.findViewById(R.id.dialog_edittext);
        mConfirm = view.findViewById(R.id.dialog_confirm);
        mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(NUMBER_LENGTH_LIMIT)});
        return view;
    }

    @Override
    protected void setDialogLayout(Context context) {
        Window dialogWindow = mDialog.getWindow();
        if (dialogWindow != null) {
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = (int) context.getResources().getDimension(R.dimen.timing_set_dialog_height);
            lp.gravity = Gravity.BOTTOM;
            dialogWindow.setAttributes(lp);
        }
    }

    public int getTimingSetDuration() {
        String content = mEditText.getText().toString();
        try {
            int duration = Integer.parseInt(content);
            if (duration > TIMING_SET_MAX) {
                return -1;
            } else {
                return Math.max(duration, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public boolean isEmpty() {
        return mEditText.getText().toString().equals("");
    }

    public View getConfirmButton() {
        return mConfirm;
    }
}